package com.wing.warehouse.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.warehouse.model.response.PalletBoxListVO;
import com.wing.warehouse.model.response.VehicleStandardSizesVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.warehouse.model.entity.PalletBoxList;
import com.wing.warehouse.model.request.PalletBoxListForm;
import com.wing.warehouse.service.PalletBoxListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-托盘料箱清单管理", tags = "#/warehouse/palletBoxList")
@RestController("palletBoxListController")
@RequestMapping("warehouse/admin/palletBoxList")
@RequiredArgsConstructor
public class PalletBoxListController {

    private final PalletBoxListService palletBoxListService;

    @ApiOperation(value = "查询所有托盘料箱清单", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "托盘编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<PalletBoxListVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String code
            ,String spaceCodeFlag
            ,Long vehicleTypeId
            ,Long warehouseId
            ,Long areaId
            ,Long shelfId
            ,String spaceCode
    ){
        Page pg = new Page(page, limit);
        Map queryParam = new HashMap();
        queryParam.put("code", code);
        queryParam.put("spaceCodeFlag", spaceCodeFlag);
        queryParam.put("vehicleTypeId", vehicleTypeId);
        queryParam.put("warehouseId", warehouseId);
        queryParam.put("areaId", areaId);
        queryParam.put("shelfId", shelfId);
        queryParam.put("spaceCode", spaceCode);
        //排序
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        
        IPage<PalletBoxListVO> list = palletBoxListService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加托盘料箱清单", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(PalletBoxListForm.Add.class) PalletBoxListForm palletBoxListForm) {
        if (palletBoxListService.add(palletBoxListForm)) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改托盘料箱清单", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(PalletBoxListForm.Update.class) PalletBoxListForm palletBoxListForm) {
        PalletBoxList palletBoxList = palletBoxListService.getById(palletBoxListForm.getId());
        if(palletBoxList == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(PalletBoxList.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(palletBoxListForm, palletBoxList, copyOptions);
        if (palletBoxListService.updateById(palletBoxList)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }


    /*@ApiOperation(value = "删除托盘料箱清单", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "palletBoxListId", value = "托盘料箱清单id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long palletBoxListId) {
        if (palletBoxListService.removeById(palletBoxListId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }*/

    @ApiOperation(value="检查唯一性")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",value = "前缀",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "id",value = "id",required = false,dataType = "Long",paramType = "query")
    })
    @GetMapping("/isOnly")
    public JsonResult isOnly(@RequestParam(name = "code",required = false) String code,
                             @RequestParam(name="id",required=false) Long id){
        QueryWrapper<PalletBoxList> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(code)){
            queryWrapper.eq("prefix", code);
        }
        if (id!=null) {
            queryWrapper.ne("id", id);
        }
        int count = palletBoxListService.count(queryWrapper);
        return JsonResult.success(count==0);
    }

    @ApiOperation(value = "打印条形码", notes = "打印条码")
    @GetMapping("/getBarCode")
    public JsonResult<String> getBarCode(@RequestParam(value = "code") String code){
        String data = palletBoxListService.getBarCode(code);
        return JsonResult.success("true",data);
    }

    @ApiOperation(value = "查看料箱信息", notes = "查看料箱信息")
    @GetMapping("/getBySpaceCode")
    public JsonResult<PalletBoxList> getBySpaceCode(@RequestParam(value = "spaceCode") String spaceCode){
        QueryWrapper<PalletBoxList> query = new QueryWrapper<>();
        query.eq("space_code", spaceCode);
        List<PalletBoxList> palletBoxList = palletBoxListService.list(query);
        return JsonResult.success(palletBoxList);
    }


}
